﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SMARTMobile.Common;
using SmartMobile.Data;
using SMARTMobile.Models;
using SMARTMobile.Models.Local;
using SMARTMobile.Models.UI;
using SMARTMobile.WinPhone;

namespace SMARTMobile.ViewModels.Advance
{
    public class CurrentOutDoorDisplayViewModel : ViewModelBase
    {
        #region Fields

        private SmartMobileDBContext _smartMobileContext;
        private ObservableCollection<CurrentInDoorDisplayExtend> _currentInDoorDoorDisplayObservable;
        private ObservableCollection<CurrentOutDoorDisplayExtend> _currentOutDoorDisplayObservable;
        private ObservableCollection<PhoneVendor> _phoneVenderObservable;
        private CurrentOutDoorDisplayExtend _selectedOutDoorDisplayObservable;
        #endregion

        public CurrentOutDoorDisplayViewModel()
        {
            this.CreateConnection();
            //this.GetPhoneVender();
            //this.GetPhonevendorVolume();
            this.GetCurrentOutDoorDisplay();

            this.SaveCommand = new CommandRoute
            {
                ExecuteHandler = this.ExecuteSaveCommand
            };

            this.ExitCommand = new CommandRoute
            {
                ExecuteHandler = this.ExecuteExitCommand
            };
        }

        #region Properties

        public ObservableCollection<CurrentInDoorDisplayExtend> CurrentInDoorDoorDisplayObservable
        {
            get { return this._currentInDoorDoorDisplayObservable; }
            set
            {
                this._currentInDoorDoorDisplayObservable = value;
                base.RaisePropertyChanged("CurrentInDoorDoorDisplayObservable");
            }
        }
        
        public ObservableCollection<CurrentOutDoorDisplayExtend> CurrentOutDoorDisplayObservable
        {
            get { return this._currentOutDoorDisplayObservable; }
            set
            {
                this._currentOutDoorDisplayObservable = value;
                base.RaisePropertyChanged("CurrentOutDoorDisplayObservable");
            }
        }

        public CurrentOutDoorDisplayExtend SelectedOutDoorDisplayObservable
        {
            get { return this._selectedOutDoorDisplayObservable; }
            set
            {
                this._selectedOutDoorDisplayObservable = value;
                base.RaisePropertyChanged("SelectedOutDoorDisplayObservable");
            }
        }


        public ObservableCollection<PhoneVendor> PhoneVendorObservable
        {
            get { return this._phoneVenderObservable; }
            set
            {
                this._phoneVenderObservable = value;
                base.RaisePropertyChanged("PhoneVenderObservable");
            }
        }

        public List<OutDoorType> OutDoorTypeList
        {
            get
            {
                var outDoorList = this._smartMobileContext.OutDoorType.ToList();
          
                return outDoorList;
            }
        }

        #endregion

        #region Command
        public ICommandRoute ExitCommand
        {
            get;

            private set;
        }

        public ICommandRoute SaveCommand
        {
            get;

            private set;
        }
        #endregion

        #region Command Action Handlers
        private void ExecuteExitCommand(object commandParams)
        {
            this.ExitCommand.RaiseCanExecuteChanged(true);
            base.PageNavigation.Navigate("InputData");
        }

        private void ExecuteSaveCommand(object commandParams)
        {
            SaveData();

            this.ShowMessage("Lưu thành công");
            this.SaveCommand.RaiseCanExecuteChanged(true);
        }
        #endregion

        #region Method

        public void SaveData()
        {
            if (GlobalInformation.SelectedTCARCUS != null)
            {
                List<CurrentOutDoorDisplay> list = this._smartMobileContext.CurrentOutDoorDisplay.Where(x => x.Customerkey == GlobalInformation.SelectedTCARCUS.Customerkey).ToList();
                this._smartMobileContext.CurrentOutDoorDisplay.DeleteAllOnSubmit(list);

                this._smartMobileContext.SubmitChanges();
            }

            // Save current out door
            List<CurrentOutDoorDisplay> currentOutDoorDisplay = new List<CurrentOutDoorDisplay>();
            foreach (var item in this.CurrentOutDoorDisplayObservable)
            {
                if (GlobalInformation.SelectedTCARCUS != null)
                {
                    CurrentOutDoorDisplay outDoor = new CurrentOutDoorDisplay
                    {
                        ID_PK = Guid.NewGuid(),
                        Customerkey = GlobalInformation.SelectedTCARCUS.Customerkey,
                        PhoneVendorCode = item.PhoneVendorCode,
                        OutDoorTypeCode = item.OutDoorTypeCode,
                        Qty = item.Qty,
                        QtyPicture = item.QtyPicture,
                        PictureURL = item.PictureURL,
                        WherePicture = item.WherePicture
                    };

                    currentOutDoorDisplay.Add(outDoor);
                }
            }

            if (currentOutDoorDisplay.Any())
            {
                this._smartMobileContext.CurrentOutDoorDisplay.InsertAllOnSubmit(currentOutDoorDisplay);
                this._smartMobileContext.SubmitChanges();
            }

            this.IsBusy = false;
        }

        public void UpdateData(string outDoorTypeCode, string phoneVendorCode, int qty)
        {
            if (GlobalInformation.SelectedTCARCUS != null)
            {
                var outdoorDisplay = (from outdoor in this._smartMobileContext.CurrentOutDoorDisplay
                                              where
                                                  outdoor.Customerkey == GlobalInformation.SelectedTCARCUS.Customerkey &&
                                                  outdoor.OutDoorTypeCode == outDoorTypeCode && outdoor.PhoneVendorCode == phoneVendorCode
                                              select outdoor).FirstOrDefault();

                if (outdoorDisplay != null)
                {
                    outdoorDisplay.Qty = qty;
                    this._smartMobileContext.SubmitChanges();
                }
            }
        }

        private void GetCurrentOutDoorDisplay()
        {
            //if (this.CurrentOutDoorDisplayObservable == null)
            //{
            //    this.CurrentOutDoorDisplayObservable = new ObservableCollection<CurrentOutDoorDisplayExtend>();
            //}

            this.CurrentOutDoorDisplayObservable = new ObservableCollection<CurrentOutDoorDisplayExtend>();

            //List<PhoneVendor> phonevendors = this._smartMobileContext.PhoneVendor.Where(v => v.IsOperator.Value == true).ToList();
            //foreach (var item in phonevendors)
            //{
            //    foreach (var outDoorType in this.OutDoorTypeList)
            //    {
            //        CurrentOutDoorDisplay currentOutDoorDisplayInDB = this._smartMobileContext.CurrentOutDoorDisplay.FirstOrDefault(
            //              a => a.Customerkey == GlobalInformation.SelectedTCARCUS.Customerkey &&
            //                   a.PhoneVendorCode == item.PhoneVendorCode && a.OutDoorTypeCode == outDoorType.OutDoorTypeCode);

            //        CurrentOutDoorDisplayExtend current = new CurrentOutDoorDisplayExtend
            //        {
            //            ID_PK = Guid.NewGuid(),
            //            Customerkey = GlobalInformation.SelectedTCARCUS.Customerkey,
            //            PhoneVendorCode = item.PhoneVendorCode,
            //            PhoneVendorName = item.PhoneVendorName,
            //            OutDoorTypeName = outDoorType.Description,
            //            OutDoorTypeCode = outDoorType.OutDoorTypeCode,
            //            Qty = outDoorType.NumberPicture,
            //            PictureURL = string.Empty,
            //            WherePicture = string.Empty
            //        };

            //        if (currentOutDoorDisplayInDB != null)
            //        {
            //            current.Qty = currentOutDoorDisplayInDB.Qty;
            //            current.PictureURL = currentOutDoorDisplayInDB.PictureURL;
            //            current.WherePicture = currentOutDoorDisplayInDB.WherePicture;
            //        }

            //        this.CurrentOutDoorDisplayObservable.Add(current);
            //    }
            //}

            if (GlobalInformation.SelectedTCARCUS != null)
            {
                List<CurrentOutDoorDisplay> currentOutDoorDisplay = this._smartMobileContext.CurrentOutDoorDisplay.Where(p => p.Customerkey == GlobalInformation.SelectedTCARCUS.Customerkey).ToList();
                if (currentOutDoorDisplay.Count > 0)
                {
                    foreach (var item in currentOutDoorDisplay)
                    {
                        CurrentOutDoorDisplayExtend current = new CurrentOutDoorDisplayExtend
                        {
                            ID_PK = item.ID_PK,
                            Customerkey = item.Customerkey,
                            PhoneVendorCode = item.PhoneVendorCode,
                            PhoneVendorName = this.GetPhoneVendorName(item.PhoneVendorCode),
                            OutDoorTypeName = this.GetOutDoorTypeName(item.OutDoorTypeCode),
                            OutDoorTypeCode = item.OutDoorTypeCode,
                            Qty = item.Qty,
                            QtyPicture = item.QtyPicture,
                            PictureURL = item.PictureURL,
                            WherePicture = item.WherePicture
                        };

                        this.CurrentOutDoorDisplayObservable.Add(current);
                    }
                }
                else
                {
                    CreateList();
                }
            }

            if (GlobalInformation.SelectedTCARCUS == null)
            {
                CreateList();
            }
        }

        public void CreateList()
        {
            List<PhoneVendor> phonevendors = this._smartMobileContext.PhoneVendor.ToList();            

            foreach (var item in phonevendors)
            {
                foreach (var outDoorType in this.OutDoorTypeList)
                {
                    CurrentOutDoorDisplayExtend current = new CurrentOutDoorDisplayExtend
                    {
                        ID_PK = Guid.NewGuid(),
                        Customerkey = GlobalInformation.SelectedTCARCUS.Customerkey,
                        PhoneVendorCode = item.PhoneVendorCode,
                        PhoneVendorName = item.PhoneVendorName,
                        OutDoorTypeName = outDoorType.Description,
                        OutDoorTypeCode = outDoorType.OutDoorTypeCode,
                        QtyPicture = outDoorType.NumberPicture,
                        PictureURL = string.Empty,
                        WherePicture = string.Empty
                    };

                    this.CurrentOutDoorDisplayObservable.Add(current);
                }
            }

            // Save current out door           
            List<CurrentOutDoorDisplay> currentOutDoorDisplay = new List<CurrentOutDoorDisplay>();
            foreach (var item in this.CurrentOutDoorDisplayObservable)
            {
                CurrentOutDoorDisplay outDoor = new CurrentOutDoorDisplay
                {
                    ID_PK = item.ID_PK,
                    Customerkey = item.Customerkey,
                    PhoneVendorCode = item.PhoneVendorCode,
                    OutDoorTypeCode = item.OutDoorTypeCode,
                    Qty = item.Qty,
                    QtyPicture = item.QtyPicture,
                    PictureURL = item.PictureURL,
                    WherePicture = item.WherePicture
                };

                currentOutDoorDisplay.Add(outDoor);
            }

            if (currentOutDoorDisplay.Any())
            {
                this._smartMobileContext.CurrentOutDoorDisplay.InsertAllOnSubmit(currentOutDoorDisplay);
                this._smartMobileContext.SubmitChanges();
            }

            //this._smartMobileContext.CurrentOutDoorDisplay.InsertAllOnSubmit(this.CurrentOutDoorDisplayObservable);
            //this._smartMobileContext.SubmitChanges();
        }

        private void GetPhoneVender()
        {
            this.PhoneVendorObservable = new ObservableCollection<PhoneVendor>(this._smartMobileContext.PhoneVendor);
        }

        private string GetPhoneVendorName(string phonevendorCode)
        {
            return this._smartMobileContext.PhoneVendor.Where(p => p.PhoneVendorCode == phonevendorCode).Select(p => p.PhoneVendorName).FirstOrDefault();
        }

        private string GetOutDoorTypeName(string outdoorTypeCode)
        {
            return this._smartMobileContext.OutDoorType.Where(p => p.OutDoorTypeCode == outdoorTypeCode).Select(p => p.Description).FirstOrDefault();
        }

        private void CreateConnection()
        {
            this._smartMobileContext = new SmartMobileDBContext(SmartMobileDBContext.ConnectionString);
            this._smartMobileContext.CreateIfNotExists();
            this._smartMobileContext.LogDebug = true;
        }
        #endregion
    }
}
